namespace Rsdn.Janus.CoreServices.Extensibility
{
    using System;
    using System.Collections.Generic;
    using JetBrains.Annotations;
    using SmartApp;

    [Service(typeof(ISharedInfoService))]
    public sealed class SharedInfoService : ISharedInfoService
    {
        private readonly IServiceProvider _serviceProvider;

        Dictionary<string,object> datas = new Dictionary<string, object>();

        object sharedLock = new object();

        /// <summary>
        /// Initializes a new instance of the <see cref="SharedInfoService"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <exception cref="ArgumentNullException"><paramref name="serviceProvider"/> is <c>null</c>.</exception>
        public SharedInfoService([NotNull] IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");   //MLHIDE
            }
        }

        #region Implementation of ISharedInfoService

        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public string GetStringValue(string key)
        {
            string ret = null;
            lock (sharedLock)
            {
                if (datas.ContainsKey(key))
                {
                    ret = datas[key] as string;
                }
            }

            return ret;
        }

        /// <summary>
        /// Gets the int.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public int? GetIntValue(string key)
        {
            int? ret = null;
            lock (sharedLock)
            {
                if (datas.ContainsKey(key))
                {
                    ret = datas[key] as int?;
                }
            }

            return ret;
        }

        public bool? GetBoolValue(string key)
        {
            bool? ret = null;
            lock (sharedLock)
            {
                if (datas.ContainsKey(key))
                {
                    ret = datas[key] as bool?;
                }
            }

            return ret;
        }

        public Enum GetEnumValue(string key)
        {
            Enum ret = null;
            lock (sharedLock)
            {
                if (datas.ContainsKey(key))
                {
                    ret = (Enum) datas[key];
                }
            }

            return ret;
        }

        public object GetObjValue(string key)
        {
            object ret = null;
            lock (sharedLock)
            {
                if (datas.ContainsKey(key))
                {
                    ret = datas[key];
                }
            }

            return ret;
        }

        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void SetValue(string key, string value)
        {
            lock (sharedLock)
            {
                datas[key] = value;
            }
        }

        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void SetValue(string key, int value)
        {
            lock (sharedLock)
            {
                datas[key] = value;
            }
        }

        public void SetValue(string key, bool value)
        {
            lock (sharedLock)
            {
                datas[key] = value;
            }
        }

        public void SetValue(string key, Enum value)
        {
            lock (sharedLock)
            {
                datas[key] = value;
            }
        }

        public void SetValue(string key, object value)
        {
            lock (sharedLock)
            {
                datas[key] = value;
            }
        }

        #endregion
    }
}